//
//  LoginView.swift
//  NewsAPP
//
//  Created by URNing B on 2021/5/19.
//

import UIKit

class LoginView: UIViewController {
    static let count = 0
    let defaults = UserDefaults.standard
    let uuidKey = "USERNAME"
    var Username:UITextField!
    var Password:UITextField!
    var btnLogin:UIButton!

    
    override func viewDidLoad() {
        super.viewDidLoad()
        let mainSize = UIScreen.main.bounds.size
        
        let vLogin = UIView(frame: CGRect(x: 30, y:200,width: mainSize.width - 60,height: 200))
        vLogin.layer.borderWidth = 0.5
        vLogin.layer.borderColor = UIColor.lightGray.cgColor
        vLogin.backgroundColor = UIColor.white
        self.view.addSubview(vLogin)
        
        Username = UITextField(frame: CGRect(x: 30, y: 30, width: vLogin.frame.size.width - 60, height: 40))
        Username.layer.cornerRadius = 5
        Username.layer.borderColor = UIColor.lightGray.cgColor
        Username.layer.borderWidth = 0.5
        Username.autocapitalizationType = .none
        Username.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        Username.leftViewMode = UITextField.ViewMode.always
        
        let imgUsername = UIImageView(frame: CGRect(x: 10, y: 10, width: 20, height: 20))
        imgUsername.image = UIImage(named: "iconfont-user")
        Username.leftView!.addSubview(imgUsername)
        let savedUsername = defaults.string(forKey: uuidKey)
        if savedUsername != nil
        {
            Username.text = savedUsername
        }
        vLogin.addSubview(Username)
        
        Password = UITextField(frame: CGRect(x: 30, y: 90, width: vLogin.frame.size.width - 60, height: 40))
        Password.layer.cornerRadius = 5
        Password.layer.borderColor = UIColor.lightGray.cgColor
        Password.layer.borderWidth = 0.5
        Password.autocapitalizationType = .none
        Password.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        Password.leftViewMode = UITextField.ViewMode.always
        
        let imgPassword = UIImageView(frame: CGRect(x: 10, y: 10, width: 20, height: 20))
        imgPassword.image = UIImage(named: "iconfont-password")
        Password.leftView!.addSubview(imgPassword)
        vLogin.addSubview(Password)
        
        btnLogin = UIButton(frame: CGRect(x: mainSize.width/2-160/2, y: 150, width: 120, height: 40))
        btnLogin.setTitle("登陆", for: .normal)
        btnLogin.backgroundColor = UIColor.gray
        vLogin.addSubview(btnLogin)
        
        btnLogin.addTarget(self, action: #selector(loginEvent), for: .touchUpInside)
    }
    @objc func loginEvent()
    {
        let username = Username.text!
        let password = Password.text!
        Username.resignFirstResponder()
        Password.resignFirstResponder()
        if username == "2019302110298" && password == "123"
        {
            let savedUsername = defaults.string(forKey: uuidKey)
            if savedUsername == nil
            {
                defaults.set(username,forKey: uuidKey)
                DBControl.initDB()
                print("数据库初始化")
            }
            let sqlite = SQLiteManager.sharedInstance
            if !sqlite.openDB(){return}
            let queryResult = sqlite.execQuerySQL(sql: "SELECT * FROM CollectionNews")
            sqlite.closeDB()
            DBControl.CollectionCount = queryResult!.count
            let mainBoard:UIStoryboard! = UIStoryboard(name: "Main", bundle: nil)
            let VCMain = mainBoard!.instantiateViewController(withIdentifier: "NewsMain")
            UIApplication.shared.windows[0].rootViewController = VCMain
        }
        else
        {
            let p = UIAlertController(title: "登陆失败", message: "用户名或密码错误", preferredStyle: .alert)
            p.addAction(UIAlertAction(title: "确定", style: .default, handler: { (
                act:UIAlertAction)in
                self.Password.text = ""
            }))
            present(p, animated: false, completion: nil)
        }
    }
}
